<template>
  <div class="SearchDialog" v-loading="loading">
    <div class="SearchDialog">
      <el-dialog title="业务查询" :visible.sync="$parent.DialogSearch" v-dialogDrag width="80%" @close="closeDialog()">
        <el-form :model="form" :inline="true" label-width="140px" ref="SearchFormInfo">
          <el-row :gutter="5">
              <el-col :span="6">
                <el-form-item label="逝者姓名：">
                    <el-input v-model="form.deadName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="逝者证件号码：" prop="certificateNo">
                    <el-input v-model="form.certificateNo"></el-input>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="6">
                  <el-form-item label="类型：" prop="remainsStateCode">
                    <el-select
                        v-model="form.remainsStateCode"
                        clearable
                        filterable
                        placeholder="请选择类型"
                        class="w150"
                    >
                        <el-option
                        v-for="item in dictionary.remains_state"
                        :key="item.dataCode"
                        :label="item.dataText"
                        :value="item.dataCode"
                        :disabled="item.disableState"
                        />
                    </el-select>
                  </el-form-item>
              </el-col> -->
              <el-col :span="6">
                  <el-button type="primary" @click="onSubmit()">查 询</el-button>
                  <el-button type="primary" plain @click="resetForm()">重 置</el-button>
                  <el-button type="primary" plain @click="$parent.DialogSearch = false">关 闭</el-button>
              </el-col>
          </el-row>
          <div class="tablewrapper">

            <div class="table">
              <h3 class="title">查询列表</h3>
              <el-table :fit="true"
                        :data="tableData"
                        :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
                        @row-dblclick="tbDbOpen"
                        border
                        style="width: 100%;">
                <el-table-column sortable align='center' prop="businessCode" label="业务编码" width="200">
                </el-table-column>
                <el-table-column
                  prop="deadName"
                  align='center'
                  label="逝者姓名"
                  width="200">
                  <template slot-scope="{row,$index}">
                    <!-- <span @click="openHome(row.businessCode)" v-if="row.asUnsettled<=0"
                          style="color:red">{{ row.deadName }}</span>
                    <span @click="openHome(row.businessCode)" v-if="row.asUnsettled>0">{{ row.deadName }}</span> -->        
                    <span @click="openHome(row.businessCode)">{{ row.deadName }}</span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="deadGender"
                  align='center'
                  label="性别"
                  width="100">
                </el-table-column>
                <el-table-column
                  prop="deadAge"
                  align='center'
                  label="年龄"
                  width="120">
                </el-table-column>
                <el-table-column
                  prop="certificateNo"
                  align='center'
                  label="证件号码"
                  >
                </el-table-column>
                <el-table-column
                  prop="cremationNo"
                  align='center'
                  label="火化证号"
                  >
                </el-table-column>
                
              </el-table>
              <!-- <div style="text-align: center;
                        height: 0px;
                        line-height: 20px;
                        font-size: 18px;">
                显示{{
                  pager.total === 0 ? 0 : (Math.ceil(pager.total / pager.pageSize) > 1 ? ((pager.currentPage - 1) * pager.pageSize + 1) : (1))
                }}到{{
                  pager.total === 0 ? 0 : ((Math.ceil(pager.total / pager.pageSize) === pager.currentPage) ? (pager.total) : (pager.currentPage * pager.pageSize))
                }}条
              </div> -->
              <p style="text-align: right;margin-top: 20px;">
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="pager.currentPage"
                  :page-size="pager.pageSize"
                  :page-sizes="[10,20,30,50]"
                  background
                  layout="total,sizes,prev, pager, next, jumper"
                  :total="pager.total">
                </el-pagination>
              </p>
            </div>
          </div>
        </el-form>

      </el-dialog>
    </div>
  </div>
</template>

<script>
import * as Type from '@/api/service'
import {dataFormate, dataFormatetmd} from '@/utils/index'
import {toFixed} from "@/utils/tofixed"
import fsIdCard from "@/api/FS1000A3TDIdCard";

export default {
  name: "SearchDialog",
  props: {
    copyCremationTypes: {
      type: Array
    },
    DialogSearch: {
      type: Boolean
    },
    searchResult: {
      type: Object,
      default: function () {
        return {
          "searchData": {
            deadName: "",//逝者姓名
            certificateNo: "",//证件号码
            cremationTypeCode: "",//火化类型code
            businessCode: "",
            appointmentTime: "",//到馆时间
            appointmentTimeEnd: "",//到馆时间最大值
          },
        }
      }
    },
    userType:{
      type:String
    },
    useDid:{
      type:Number,
      value:1
    },
    excludedCurrentSaveDeadFlag:{
      type:Boolean,
      value:false
    },
    searchHistoryDeadFlag:{
      type:Boolean,
      value:true
    }
  },
  data() {
    return {
      dictionary: {},
      loading: false,//加载进度
      searchFlag: false,     //保存过程中不可更改,true为正在保存
      search: "",
      //查询form表单
      form: {
        deadName: "",          //逝者姓名
        certificateNo: "",    //逝者证件号码
        cremationTypeCode: "",//火化类型编码
        remainsStateCode: "",//遗体状态编码
       
        pageSize: 0,        //每页显示记录条数
        currentPage: 0,     //当前页数
      },
      formLabelWidth: '120px',
      //分页
      pager: {
        "pageSize": 20,
        "total": 1,
        "currentPage": 1
      },
      //火化类型
      cremationTypes: {
        type: Array
      }//是否到馆登记
      , YesOrNo: [
        {label: "否", value: 0},
        {label: "是", value: 1},
        {label: "全部", value: 2},
      ],


      tableData: [],

    }
  },
  watch: {
    '$parent.DialogSearch'(val) {
      if(val){
        this.onSubmit();
      }
    }
  },
  methods: {
    // 逝者身份证读取
    fs1000A3tdReadDeadIdCard(type) {
      //读取慧影身份证读卡器的内容
      //读取iccard的数据
      fsIdCard.FsIdCard.ReadIDCard();
      let this_read = this;
      //延迟加载执行函数
      setTimeout(function () {
        let res = fsIdCard.FsIdCard.getMsg();
        console.log(JSON.stringify(res));
        //this_read.ruleForm.funeralDead.deadName = res.strSex;
        //开始处理
        //逝者姓名
        this_read.form.deadName = (res && res.name).replace(/^\s*(.*?)[\s\n]*$/g, "$1");
        // 逝者证件号码
        this_read.form.certificateNo = res && res.idCardNo;
        //结束处理
      }, 1000);
    },
    //慧影设备加载
    fs1000A3tdOnLoding() {
      fsIdCard.FsIdCard.onLoad();
    },
    fs1000A3tdRead() {
      //读iccard
      fsIdCard.FsIdCard.ReadIDCard();
    },
    fs1000A3tdStop() {
      fsIdCard.FsIdCard.Stop();
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pager.pageSize = size;
      this.onSubmit()
    },
    handleCurrentChange: function (currentPage) {
      this.pager.currentPage = currentPage;
      console.log('currentPage:' + currentPage)
      this.onSubmit()
    },
    //表单重置
    resetForm: function () {
      this.form = {
        deadName: "",          //逝者姓名
        certificateNo: "",    //逝者证件号码
        cremationTypeCode: "",//火化类型
        agentName: "",        //承办人姓名
        agentCertificateNo: "",//承办人证件号码
        agentRelation: "",     //承办人联系号码
        asSettled: "",        //是否到馆
        deathCause: "",       //死亡原因
        arrivedTimeStart: "",//入馆开始时间
        arrivedTimeEnd: "", //入馆结束时间
        pageSize: 0,        //每页显示记录条数
        currentPage: 0,     //当前页数
      };
    },
    //查询列表方法
    onSubmit(value) {
      this.loading = true
      if (value) {
        this.pager.currentPage = 1;
      }
      //定义表单查询参数
      let postData = {
        "deadName": this.form.deadName,
        "certificateNo": this.form.certificateNo,
        //火化类型编码
        // "cremationTypeCode": this.form.cremationTypeCode,
        //遗体状态编码
        "remainsStateCode": this.form.remainsStateCode,
        //死亡原因
        "deathCause": this.form.deathCause,
        //承办人姓名
        "memberOrAgentName": this.form.agentName,
        //承办人证件号
        "agentCertificateNo": this.form.agentCertificateNo,
        //承办人联系方式
        "memberOrAgentPhone": this.form.agentRelation,
        //是否结算
        "asSettled": this.form.asSettled,
        //是否冷藏
        "asCold": this.form.asCold,
        // "arrivedTimeStart": dataFormatetmd(new Date(this.form.arrivedTimeStart)),
        // "arrivedTimeEnd": dataFormatetmd(new Date(this.form.arrivedTimeEnd)),
        "settleStartTime": dataFormatetmd(new Date(this.form.settleStartTime)),
        "settleEndTime": dataFormatetmd(new Date(this.form.settleEndTime)),
        "pageSize": this.pager.pageSize,
        "currentPage": this.pager.currentPage,
        "orderField	": "",     //排序字段
        "orderType": "",        //排序顺序
        'userType':this.userType,//用户类型
        "useDid":this.useDid,
        "excludedCurrentSaveDeadFlag":this.excludedCurrentSaveDeadFlag,
        'searchHistoryDeadFlag':this.searchHistoryDeadFlag

      }
      //查询业务列表的接口
      Type['loadBusinessList'](postData).then(response => {
        const {data} = response;
        if (!data) {
          this.loading = false;
          this.$message('数据读取失败')
          return
        }
        this.tableData = data.list
        this.pager = data.pager
        this.loading = false
        // console.log('this.pager:' + JSON.stringify(data.pager))
        //   //数据读取成功
      }).catch(error => {
        this.$message(error)
        this.loading = false
        this.tableData = []
      })
    },
    //打开逝者档案
    openHome(code) {
      //给父页面赋值
      this.$parent.businessCode = code;
      //调用父页面的查询方法
      this.$parent.searchData();
      this.$parent.DialogSearch = false
    },
    tbDbOpen(row, column, cell, event) {
      this.openHome(row.businessCode);
    }
    ,
    //关闭对话框
    closeDialog() {
      //this.tableData=[]
      this.$parent.DialogSearch = false
    },
    filterHandler(value, row, column) {
      // console.log(column)
      const property = column['property'];
      return row[property] === value;
    },
    /*  loadDictionary(){
        let postData = ["cremation_type"];
        Type["loadDictionary"](postData)
          .then((response) => {
            const { data } = response;
            if (!data) {
              this.$message("加载数据失败");
              return;
            }
            this.dictionary =data;
            // this.$set(this.copydictionary.negotiator_user,this.copydictionary.negotiator_user)
          })
          .catch((error) => {
            this.$message(error);
          });
      },*/

  }, created() {
    //加载字典数据
    //this.loadDictionary();
    this.form.asSettled = this.YesOrNo[2].value; //设置默认值
    this.form.asCold = this.YesOrNo[2].value; //设置默认值
  },
  mounted() {
    this.$store.dispatch('user/getdictionary').then((res) => {     //获取字典数据
      this.dictionary = res;
      this.$set(this.dictionary, res)
    }).catch(() => console.log('promise catch err'));

    this.form.arrivedTimeStart = new Date();//到馆时间默认当前时间
    this.form.arrivedTimeEnd = new Date();
    this.onSubmit();
    //按钮权限
    // this.btnRight = this.$route.meta.buttons;

  },
  computed: {
    // tables: function () {
    //   const search = this.search
    //   if (search) {
    //     return this.ruleForm.charges.goodsServices.filter(dataNews => {
    //       return Object.keys(dataNews).some(key => {
    //         return String(dataNews[key]).toLowerCase().indexOf(search) > -1
    //       })
    //     })
    //   }
    //   return this.ruleForm.charges.goodsServices
    // }
  },
}
</script>

<style>

.SearchDialog .el-dialog__title, .SearchDialog .el-dialog__headerbtn .el-dialog__close {
  color: white;
}

.SearchDialog .el-input, .SearchDialog .el-select, .SearchDialog .el-form-item__content {
  width: 200px;
}

.SearchDialog .el-textarea__inner {
  min-width: 200px;
  max-width: 870px;
}

.SearchDialog .el-dialog {
  position: absolute;
  left: 10%;
  z-index: 99999;
}

.PrintReplace .SearchDialog.h100, .TotalMoney .SearchDialog.h100 {
  height: auto;
}
</style>
<style scoped>

.el-table /deep/ .el-table__empty-text{
    min-height: 200px;
}
</style>
